﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DGSFramework;
using System.Web.Security;
using System.IO;
using CodeCarvings.Piczard.Web;
using CodeCarvings.Piczard;
using System.Web.Services;
using System.Xml.Linq;
using CodeCarvings.Piczard.Filters.Watermarks;

namespace Framework.WebmasterTool.Photos
{
    public partial class PhotoAdd : System.Web.UI.Page
    {
        DGSFrameworkEntities entity = new DGSFrameworkEntities();

        protected void Page_Load(object sender, EventArgs e)
        {
            //txtRealeaseDate.Attributes.Add("readonly", "readonly");
            //txtExpireDate.Attributes.Add("readonly", "readonly");
            siuImage.ImageRemove += new EventHandler(siuImage_ImageRemove);
            siuImage.ImageUpload += new SimpleImageUpload.ImageUploadEventHandler(siuImage_ImageUpload);
            if (!IsPostBack)
            {
                CheckPermission();
                CallCkeditors();
                //CleanImageUploadTemp();
                CallImageUpload();
                LoadCateDDL();
                LoadPhotoDetails();
            }
        }

        public void CheckPermission()
        {
            MembershipUser someUser = Membership.GetUser();
            if (someUser == null)
            {
                Response.Redirect(Globals.ApplicationPath + "LoginAdmin/Default.aspx");
            }
            bool flag = false;
            var user = entity.aspnet_Users.Where(us => us.UserName == someUser.UserName).FirstOrDefault();
            user.aspnet_Roles.Load();
            var rolesForUser = user.aspnet_Roles.ToList();
            foreach (var item in rolesForUser)
            {
                var permission = entity.udv_Permissions.Where(ps => ps.RoleId == item.RoleId).FirstOrDefault();
                if (permission.GalleryPhoto.Value == true)
                {
                    flag = true;
                }
            }
            if (flag == false)
            {
                lblPermissionMessage.Visible = true;
                Panel1.Visible = false;
            }
        }

        public void CallCkeditors()
        {
            Globals.CallCKFinder(txtBody);
            Globals.CallCKFinder(txtBody_en);
        }

        void siuImage_ImageRemove(object sender, EventArgs e)
        {
            chkWatermarkImage.Enabled = true;
        }

        void siuImage_ImageUpload(object sender, SimpleImageUpload.ImageUploadEventArgs args)
        {
            chkWatermarkImage.Enabled = false;
        }

        public void CleanImageUploadTemp()
        {
            foreach (var file in Directory.GetFiles(Server.MapPath(ConfigPaths.Path_ImageUploadTemp_Folder_MapPath)))
            {
                File.Delete(file);
            }
        }

        public void ImageUpload(SimpleImageUpload ctrImageUpload)
        {
            ctrImageUpload.ImageBackColorApplyMode = PictureTrimmerImageBackColorApplyMode.DoNotApply;
            ctrImageUpload.CropConstraint = new FreeCropConstraint();
            ctrImageUpload.PreviewFilter = new FixedResizeConstraint(200, 200, System.Drawing.Color.Black);
            ctrImageUpload.Text_BrowseButton = "Upload ảnh";
            ctrImageUpload.Text_CancelUploadButton = "Huỷ";
            ctrImageUpload.Text_EditButton = "Sửa ảnh";
            ctrImageUpload.Text_RemoveButton = "Xoá ảnh";
        }

        public void CallImageUpload()
        {
            ImageUpload(siuImage);
        }

        public void LoadCateDDL()
        {
            string levels = "|---";
            string spaces = "&nbsp;";
            List<udv_Categories> allCate = entity.udv_Categories.ToList();
            List<udv_Categories> cateBannerParent = allCate.Where(ca => ca.ParentID == ConfigPaths.Category_Photo_ID).ToList();
            foreach (var item in cateBannerParent)
            {
                ddlCategories.Items.Add(new ListItem(item.Title, item.CateID.ToString()));
                LoadSubCateDDL(ref allCate, item.CateID, ref spaces, levels);
            }
        }

        public void LoadSubCateDDL(ref List<udv_Categories> allCate, int cateParentID, ref string spaces, string levels)
        {
            List<udv_Categories> subCateBanner = allCate.Where(ca => ca.ParentID == cateParentID).ToList();
            foreach (var item in subCateBanner)
            {
                ddlCategories.Items.Add(new ListItem(HttpUtility.HtmlDecode(spaces) + levels + item.Title, item.CateID.ToString()));
                if (entity.udv_Categories.Where(ca => ca.ParentID == item.CateID).ToList().Count > 0)
                {
                    spaces += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                    LoadSubCateDDL(ref allCate, item.CateID, ref spaces, levels);
                }
            }
            if (spaces.Length > 6)
                spaces = spaces.Substring(0, spaces.Length - 36);
        }

        public void LoadPhotoDetails()
        {
            if (Request.QueryString["PhotoID"] != null)
            {
                int photoID;
                int.TryParse(Request.QueryString["PhotoID"], out photoID);
                var photoByID = entity.udv_Photos.Include("udv_Categories").Where(pt => pt.PhotoID == photoID).FirstOrDefault();
                if (photoByID != null)
                {
                    LoadListImagePhoto(photoByID.PhotoID);
                    ddlCategories.SelectedValue = photoByID.udv_Categories.CateID.ToString();
                    txtTitle.Text = photoByID.Title.Trim();
                    txtTitle_en.Text = photoByID.Title_en.Trim();
                    txtAbstract.Text = photoByID.Abstract;
                    txtAbstract_en.Text = photoByID.Abstract_en;
                    txtMetaDescription.Text = photoByID.MetaDescription.Trim();
                    txtMetaKeywords.Text = photoByID.MetaKeywords.Trim();
                    if (photoByID.ImageUrl != null && photoByID.ImageUrl.Trim() != "")
                    {
                        string sourceImageFilePath = ConfigPaths.Path_ImagePhoto_Folder_MapPath + photoByID.ImageUrl;
                        siuImage.LoadImageFromFileSystem(Server.MapPath(sourceImageFilePath));
                        chkWatermarkImage.Enabled = false;
                    }
                    txtImageAlt.Text = photoByID.ImageAlt.Trim();
                    txtBody.Text = photoByID.Body;
                    txtBody_en.Text = photoByID.Body_en;
                    txtDisplayOrder.Text = photoByID.DisplayOrder.Value.ToString();
                    txtRealeaseDate.Text = String.Format("{0:dd/MM/yyyy hh:mm:ss tt}", photoByID.ReleaseDate);
                    txtExpireDate.Text = String.Format("{0:dd/MM/yyyy hh:mm:ss tt}", photoByID.ExpireDate);
                    chkCommentsEnable.Checked = photoByID.CommentsEnabled.Value;
                    chkApprovedComments.Checked = photoByID.ApprovedComments.Value;
                    chkOnlyForMember.Checked = photoByID.OnlyForMembers.Value;
                    chkIsApproved.Checked = photoByID.IsApproved.Value;
                    chkTopNews.Checked = photoByID.StatusID == 1 ? true : false;
                    lblAddedDate.Text = String.Format("{0:dd/MM/yyyy HH:mm:ss tt}", photoByID.AddedDate);
                    lblAddedBy.Text = photoByID.AddedBy;
                    lblUpdatedDate.Text = String.Format("{0:dd/MM/yyyy HH:mm:ss tt}", photoByID.UpdatedDate);
                    lblUpdatedBy.Text = photoByID.UpdatedBy;
                    lblTotalRating.Text = photoByID.TotalRating.ToString();
                    lblViewCount.Text = photoByID.ViewCount.ToString();
                }
                else
                {
                    Response.Redirect("Default.aspx");
                }
            }
        }

        public void LoadListImagePhoto(int photoID)
        {
            List<udv_PhotoImages> listImagePhoto = entity.udv_PhotoImages.Where(ip => ip.udv_Photos.PhotoID == photoID).ToList();
            dtlImage.DataSource = listImagePhoto;
            dtlImage.DataBind();
        }

        [WebMethod]
        public static void DeleteImagePhoto(string ImageID)
        {
            DGSFrameworkEntities entity = new DGSFrameworkEntities();
            int imageID = int.Parse(ImageID);
            udv_PhotoImages imagePhoto = entity.udv_PhotoImages.Where(ip => ip.ImageID == imageID).FirstOrDefault();
            if (imagePhoto != null)
            {
                Globals.DelelteFile(HttpContext.Current.Server.MapPath(ConfigPaths.Path_ImagePhoto_Folder_MapPath + imagePhoto.ImageUrl));
                entity.DeleteObject(imagePhoto);
                entity.SaveChanges();
            }
        }

        [WebMethod]
        public static void EditAttributeImagePhoto(string ImageID, string NewValue, string Attribute)
        {
            DGSFrameworkEntities entity = new DGSFrameworkEntities();
            int imageID = int.Parse(ImageID);
            udv_PhotoImages imagePhoto = entity.udv_PhotoImages.Where(ip => ip.ImageID == imageID).FirstOrDefault();
            string value = NewValue;
            string attribute = Attribute;
            if (imagePhoto != null)
            {
                if (attribute.Trim() == "title")
                {
                    imagePhoto.Title = value;
                }
                else if (attribute.Trim() == "title_en")
                {
                    imagePhoto.Title_en = value;
                }
                else if (attribute.Trim() == "alt")
                {
                    imagePhoto.ImageAlt = value;
                }
                entity.SaveChanges();
            }
        }

        protected void chkWatermarkImage_CheckedChanged(object sender, EventArgs e)
        {
            if (chkWatermarkImage.Checked == true && siuImage.HasImage == false)
            {
                XDocument xDocWatermark = XDocument.Load(Server.MapPath(ConfigPaths.Path_Watermark_Xml));
                XElement xEImage = xDocWatermark.Descendants("img").FirstOrDefault();
                siuImage.PostProcessingFilter = new ImageWatermark(Server.MapPath(ConfigPaths.Path_ImageUpload_Folder_MapPath + xEImage.Value), System.Drawing.ContentAlignment.BottomRight);
            }
            else if (chkWatermarkImage.Checked == false && siuImage.HasImage == false)
            {
                siuImage.PostProcessingFilter = null;
            }
        }

        public List<string> UploadAndGetImageName()
        {
            List<string> strFileName = new List<string>();
            HttpFileCollection hfc = Request.Files;
            if (hfc.Count > 0)
            {
                for (int i = 0; i < hfc.Count; i++)
                {
                    HttpPostedFile hpf = hfc[i];
                    if (hpf.FileName.Trim() != "")
                    {
                        string strFileNameTemp = Globals.GetFileName(hpf.FileName);
                        strFileName.Add(strFileNameTemp);
                        if (chkWatermarkImage2.Checked == false)
                            hpf.SaveAs(Server.MapPath(ConfigPaths.Path_ImagePhoto_Folder_MapPath + strFileNameTemp));
                        else
                        {
                            XDocument xDocWatermark = XDocument.Load(Server.MapPath(ConfigPaths.Path_Watermark_Xml));
                            XElement xEImage = xDocWatermark.Descendants("img").FirstOrDefault();
                            Globals.CreateWatermark(hpf, Server.MapPath(ConfigPaths.Path_ImagePhoto_Folder_MapPath + strFileNameTemp), Server.MapPath(ConfigPaths.Path_ImageUpload_Folder_MapPath + xEImage.Value));
                        }
                    }
                }
            }
            return strFileName;
        }

        public List<string> GetImageTitle()
        {
            List<string> listTitle = new List<string>();
            for (int i = 0; i < int.Parse(hdfNumImage.Value); i++)
            {
                if (Request.Form["Title" + i.ToString()] != null)
                {
                    listTitle.Add(Request.Form["Title" + i.ToString()].Trim());
                }
            }
            return listTitle;
        }

        public List<string> GetImageTitle_en()
        {
            List<string> listTitle = new List<string>();
            for (int i = 0; i < int.Parse(hdfNumImage.Value); i++)
            {
                if (Request.Form["Title_en" + i.ToString()] != null)
                {
                    listTitle.Add(Request.Form["Title_en" + i.ToString()].Trim());
                }
            }
            return listTitle;
        }

        public List<string> GetImageAlt()
        {
            List<string> listAlt = new List<string>();
            for (int i = 0; i < int.Parse(hdfNumImage.Value); i++)
            {
                if (Request.Form["Alt" + i.ToString()] != null)
                {
                    listAlt.Add(Request.Form["Alt" + i.ToString()].Trim());
                }
            }
            return listAlt;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            int cateID = int.Parse(ddlCategories.SelectedValue);
            udv_Categories cateForPhoto = entity.udv_Categories.Where(ca => ca.CateID == cateID).FirstOrDefault();
            int countChildCate = entity.udv_Categories.Where(ca => ca.ParentID == cateID).ToList().Count;
            if (countChildCate > 0)
            {
                lblMsg.Text = "Danh mục này có danh mục con nên không thể có bài viết";
                lblMsg.ForeColor = System.Drawing.Color.Red;
                return;
            }
            List<string> listImageNameUpload = UploadAndGetImageName();
            List<string> listImageTitle = new List<string>();
            List<string> listImageTitle_en = new List<string>();
            List<string> listImageAlt = new List<string>();
            if (listImageNameUpload.Count > 0)
            {
                listImageTitle = GetImageTitle();
                listImageTitle_en = GetImageTitle_en();
                listImageAlt = GetImageAlt();
            }
            if (Request.QueryString["PhotoID"] == null)
            {
                udv_Photos photoInsert = new udv_Photos();
                photoInsert.Title = txtTitle.Text.Trim();
                photoInsert.Title_en = txtTitle_en.Text.Trim();
                photoInsert.Abstract = txtAbstract.Text;
                photoInsert.Abstract_en = txtAbstract_en.Text;
                photoInsert.Body = txtBody.Text;
                photoInsert.Body_en = txtBody_en.Text;
                photoInsert.udv_Categories = cateForPhoto;
                if (siuImage.HasImage == true)
                {
                    string fileNameImage = Globals.GetFileName(siuImage.SourceImageClientFileName);
                    siuImage.SaveProcessedImageToFileSystem(ConfigPaths.Path_ImagePhoto_Folder + fileNameImage);
                    photoInsert.ImageUrl = fileNameImage;
                }
                else
                {
                    photoInsert.ImageUrl = "";
                }
                photoInsert.ImageAlt = txtImageAlt.Text.Trim();
                if (txtRealeaseDate.Text.Trim() != "")
                {
                    photoInsert.ReleaseDate = DateTime.ParseExact(txtRealeaseDate.Text.Trim(), "dd/MM/yyyy hh:mm:ss tt", null);
                }
                if (txtExpireDate.Text.Trim() != "")
                {
                    photoInsert.ExpireDate = DateTime.ParseExact(txtExpireDate.Text.Trim(), "dd/MM/yyyy hh:mm:ss tt", null);
                }
                photoInsert.StatusID = chkTopNews.Checked == true ? 1 : 0;
                photoInsert.CommentsEnabled = chkCommentsEnable.Checked;
                photoInsert.ApprovedComments = chkApprovedComments.Checked;
                photoInsert.OnlyForMembers = chkOnlyForMember.Checked;
                photoInsert.TotalRating = 0;
                photoInsert.ViewCount = 0;
                photoInsert.DisplayOrder = int.Parse(txtDisplayOrder.Text);
                photoInsert.AddedDate = DateTime.Now;
                photoInsert.AddedBy = Membership.GetUser().UserName;
                photoInsert.UpdatedDate = DateTime.Now;
                photoInsert.UpdatedBy = Membership.GetUser().UserName;
                photoInsert.IsApproved = chkIsApproved.Checked;
                photoInsert.MetaDescription = txtMetaDescription.Text.Trim();
                photoInsert.MetaKeywords = txtMetaKeywords.Text.Trim();
                entity.AddToudv_Photos(photoInsert);
                entity.SaveChanges();
                if (listImageNameUpload.Count > 0)
                {
                    for (int i = 0; i < listImageNameUpload.Count; i++)
                    {
                        udv_PhotoImages imagePhoto = new udv_PhotoImages();
                        imagePhoto.udv_Photos = photoInsert;
                        imagePhoto.ImageUrl = listImageNameUpload[i];
                        imagePhoto.ImageAlt = listImageAlt[i];
                        imagePhoto.Title = listImageTitle[i];
                        imagePhoto.Title_en = listImageTitle_en[i];
                        imagePhoto.Description = "";
                        imagePhoto.Description_en = "";
                        imagePhoto.AddedDate = DateTime.Now;
                        entity.AddToudv_PhotoImages(imagePhoto);
                        entity.SaveChanges();
                    }
                }
                string urlRewritePhoto = Globals.CreateUrlRewritePhoto(ref entity, photoInsert.PhotoID, true);
                photoInsert.UrlRewrite = urlRewritePhoto;
                entity.SaveChanges();
                Globals.CreateROR("~/ror.xml", urlRewritePhoto + DGSFramework.Globals.GetUrlRewriteExtension(), photoInsert.Title, photoInsert.MetaDescription);
                Globals.CreateSiteMap("~/sitemap.xml", urlRewritePhoto + DGSFramework.Globals.GetUrlRewriteExtension());
                Globals.CreateUrlList("~/urllist.txt", urlRewritePhoto + DGSFramework.Globals.GetUrlRewriteExtension());

                //create db log
                photoInsert.udv_CategoriesReference.Load();
                CreateDBLog(photoInsert.PhotoID, photoInsert.udv_Categories.CateID, TypeAction.Insert, "Tạo bài viết hình ảnh: " + photoInsert.Title, photoInsert.Abstract, photoInsert.Body, photoInsert.ReleaseDate != null ? string.Format("{0:dd/MM/yyyy} HH:mm:ss", photoInsert.ReleaseDate) : "",
                    photoInsert.ExpireDate != null ? string.Format("{0:dd/MM/yyyy} HH:mm:ss", photoInsert.ExpireDate) : "", photoInsert.IsApproved.Value.ToString(), photoInsert.StatusID.Value.ToString(), photoInsert.MetaDescription, photoInsert.MetaKeywords);

                //data cache
                Cache.Remove("Photo");
                //redirect page
                Response.Redirect("Default.aspx");
            }
            else
            {
                int photoID;
                int.TryParse(Request.QueryString["PhotoID"].ToString(), out photoID);
                udv_Photos photoUpdate = entity.udv_Photos.Where(pt => pt.PhotoID == photoID).FirstOrDefault();
                if (photoUpdate != null)
                {
                    string urlRewritePhotoOld = Globals.CreateUrlRewritePhoto(ref entity, photoUpdate.PhotoID, true);
                    string urlRewritePhotoNew;
                    string oldTitle = photoUpdate.Title;
                    photoUpdate.Title = txtTitle.Text.Trim();
                    photoUpdate.Title_en = txtTitle_en.Text.Trim();
                    photoUpdate.Abstract = txtAbstract.Text;
                    photoUpdate.Abstract_en = txtAbstract_en.Text;
                    photoUpdate.Body = txtBody.Text;
                    photoUpdate.Body_en = txtBody_en.Text;
                    photoUpdate.udv_Categories = cateForPhoto;
                    if (siuImage.HasNewImage == true)
                    {
                        Globals.DelelteFile(Server.MapPath(ConfigPaths.Path_ImagePhoto_Folder_MapPath + photoUpdate.ImageUrl));
                        string fileNameImage = Globals.GetFileName(siuImage.SourceImageClientFileName);
                        siuImage.SaveProcessedImageToFileSystem(ConfigPaths.Path_ImagePhoto_Folder + fileNameImage);
                        photoUpdate.ImageUrl = fileNameImage;
                    }
                    else if (siuImage.HasImage == false)
                    {
                        Globals.DelelteFile(Server.MapPath(ConfigPaths.Path_ImagePhoto_Folder_MapPath + photoUpdate.ImageUrl));
                        photoUpdate.ImageUrl = "";
                    }
                    photoUpdate.ImageAlt = txtImageAlt.Text.Trim();
                    if (txtRealeaseDate.Text.Trim() != "")
                    {
                        photoUpdate.ReleaseDate = DateTime.ParseExact(txtRealeaseDate.Text.Trim(), "dd/MM/yyyy hh:mm:ss tt", null);
                    }
                    else
                    {
                        photoUpdate.ReleaseDate = null;
                    }
                    if (txtExpireDate.Text.Trim() != "")
                    {
                        photoUpdate.ExpireDate = DateTime.ParseExact(txtExpireDate.Text.Trim(), "dd/MM/yyyy hh:mm:ss tt", null);
                    }
                    else
                    {
                        photoUpdate.ExpireDate = null;
                    }
                    photoUpdate.StatusID = chkTopNews.Checked == true ? 1 : 0;
                    photoUpdate.CommentsEnabled = chkCommentsEnable.Checked;
                    photoUpdate.ApprovedComments = chkApprovedComments.Checked;
                    photoUpdate.OnlyForMembers = chkOnlyForMember.Checked;
                    photoUpdate.DisplayOrder = int.Parse(txtDisplayOrder.Text);
                    photoUpdate.UpdatedDate = DateTime.Now;
                    photoUpdate.UpdatedBy = Membership.GetUser().UserName;
                    photoUpdate.IsApproved = chkIsApproved.Checked;
                    photoUpdate.MetaDescription = txtMetaDescription.Text.Trim();
                    photoUpdate.MetaKeywords = txtMetaKeywords.Text.Trim();
                    entity.SaveChanges();
                    if (listImageNameUpload.Count > 0)
                    {
                        for (int i = 0; i < listImageNameUpload.Count; i++)
                        {
                            udv_PhotoImages imagePhoto = new udv_PhotoImages();
                            imagePhoto.udv_Photos = photoUpdate;
                            imagePhoto.ImageUrl = listImageNameUpload[i];
                            imagePhoto.ImageAlt = listImageAlt[i];
                            imagePhoto.Title = listImageTitle[i];
                            imagePhoto.Title_en = listImageTitle_en[i];
                            imagePhoto.Description = "";
                            imagePhoto.Description_en = "";
                            imagePhoto.AddedDate = DateTime.Now;
                            entity.AddToudv_PhotoImages(imagePhoto);
                            entity.SaveChanges();
                        }
                    }
                    urlRewritePhotoNew = Globals.CreateUrlRewritePhoto(ref entity, photoUpdate.PhotoID, true);
                    photoUpdate.UrlRewrite = urlRewritePhotoNew;
                    entity.SaveChanges();
                    Globals.UpdateROR("~/ror.xml", urlRewritePhotoOld + DGSFramework.Globals.GetUrlRewriteExtension(), urlRewritePhotoNew + DGSFramework.Globals.GetUrlRewriteExtension(), photoUpdate.Title, photoUpdate.MetaDescription);
                    Globals.UpdateSiteMap("~/sitemap.xml", urlRewritePhotoOld + DGSFramework.Globals.GetUrlRewriteExtension(), urlRewritePhotoNew + DGSFramework.Globals.GetUrlRewriteExtension());
                    Globals.UpdateUrlList("~/urllist.txt", urlRewritePhotoOld + DGSFramework.Globals.GetUrlRewriteExtension(), urlRewritePhotoNew + DGSFramework.Globals.GetUrlRewriteExtension());
                    //data cache
                    Cache.Remove("Photo");

                    //create db log
                    CreateDBLog(photoUpdate.PhotoID, photoUpdate.udv_Categories.CateID, TypeAction.Update, "Cập nhật bài viết hình ảnh: " + oldTitle + " - " + photoUpdate.Title, photoUpdate.Abstract, photoUpdate.Body, photoUpdate.ReleaseDate != null ? string.Format("{0:dd/MM/yyyy} HH:mm:ss", photoUpdate.ReleaseDate) : "",
                        photoUpdate.ExpireDate != null ? string.Format("{0:dd/MM/yyyy} HH:mm:ss", photoUpdate.ExpireDate) : "", photoUpdate.IsApproved.Value.ToString(), photoUpdate.StatusID.Value.ToString(), photoUpdate.MetaDescription, photoUpdate.MetaKeywords);

                    //redirect page
                    Response.Redirect("Default.aspx");
                }
                else
                {
                    lblMsg.Text = "Bài viết muốn cập nhật không tồn tại";
                    lblMsg.ForeColor = System.Drawing.Color.Red;
                }
            }
        }

        public void CreateDBLog(int? postID, int? postCateID, TypeAction typeAction, string title, string _abstract, string description,
        string releaseDate, string expireDate, string isApproved, string statusID, string metaDescription, string metaKeywords)
        {
            udv_Log log = new udv_Log();
            log.PostID = postID;
            log.PostCateID = postCateID;
            log.TypeAction = (int)typeAction;
            log.Title = title;
            log.Abstract = _abstract;
            log.Description = description;
            log.ReleaseDate = releaseDate;
            log.ExpireDate = expireDate;
            log.IsApproved = isApproved;
            log.StatusID = statusID;
            log.MetaDescription = metaDescription;
            log.MetaKeywords = metaKeywords;
            log.ActionedDate = DateTime.Now;
            log.ActionBy = Membership.GetUser().UserName;
            log.IpAdress = Globals.GetClientIPAddress();

            entity.AddToudv_Log(log);
            entity.SaveChanges();
        }
    }
}